﻿namespace ToRTalent {
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Talents;

    public partial class MainPage : UserControl {
        /// <summary>
        /// Initializes a new instance of MainPage
        /// </summary>
        public MainPage() {
            InitializeComponent();
            Address.ParseAddress();
            ChangeAddress();
            SetHandlers();
            PtsAvailable = MAX_PTS;
        }

        private void SetHandlers() {
            TreeOne.TreePanel.TalentsChanged += new EventHandler( TalentsChanged );
            TreeOne.TreePanel.TalentsReset += new EventHandler( TalentTreeReset );
            TreeTwo.TreePanel.TalentsChanged += new EventHandler( TalentsChanged );
            TreeTwo.TreePanel.TalentsReset += new EventHandler( TalentTreeReset );
            TreeThree.TreePanel.TalentsChanged += new EventHandler( TalentsChanged );
            TreeThree.TreePanel.TalentsReset += new EventHandler( TalentTreeReset );
        }

        private int MAX_PTS = 41;
        internal static int PtsAvailable { get; set; }

        private static TalentBase _talents;
        internal static TalentBase MainTalents { get { return _talents; } }

        internal void TalentsChanged( object sender, EventArgs e ) {
            int totalPts = TreeOne.TreePanel.GetAllPoints() + TreeTwo.TreePanel.GetAllPoints() + TreeThree.TreePanel.GetAllPoints();
            PtsAvailable = MAX_PTS - totalPts;
            //numberRemaining.Content = PtsAvailable;
            TreeOne.TreeNameLabel.Content = TreeOne.TreeName + " (" + TreeOne.TreePanel.GetAllPoints() + ")";
            TreeTwo.TreeNameLabel.Content = TreeTwo.TreeName + " (" + TreeTwo.TreePanel.GetAllPoints() + ")";
            TreeThree.TreeNameLabel.Content = TreeThree.TreeName + " (" + TreeThree.TreePanel.GetAllPoints() + ")";
            ChangeAddress();
        }

        internal void TalentTreeReset( object sender, EventArgs e ) {
            PtsAvailable = MAX_PTS - ( _talents.TreeCounts[0] + _talents.TreeCounts[1] + _talents.TreeCounts[2] );
            //numberRemaining.Content = PtsAvailable;
        }

        private void SetClassTalents( TalentBase tal, bool isRepub ) {
            _talents = null;
            _talents = tal;
            PtsAvailable = MAX_PTS;
            setTreeTalents();
            setTreeNames();
            SetIsRepublic( isRepub );
            //numberRemaining.Content = PtsAvailable;
        }

        private void setTreeTalents() {
            TreeOne.TreePanel.CharacterTalents = _talents;
            TreeTwo.TreePanel.CharacterTalents = _talents;
            TreeThree.TreePanel.CharacterTalents = _talents;
        }

        private void setTreeNames() {
            TreeOne.TreeName = TreeOne.TreePanel.TreeName;
            TreeTwo.TreeName = TreeTwo.TreePanel.TreeName;
            TreeThree.TreeName = TreeThree.TreePanel.TreeName;
        }

        private void SetIsRepublic( bool isRepublic ) {
            TreeOne.IsRepublic = isRepublic;
            TreeTwo.IsRepublic = isRepublic;
            TreeThree.IsRepublic = isRepublic;
            progressBar.IsRepublic = isRepublic;
        }

        

        private void ResetButton_MouseEnter( object sender, MouseEventArgs e ) {
            ResetButton.Source = new BitmapImage( new Uri( @"Images/Buttons/ResetAllBtnHover.png", UriKind.Relative ) );
        }

        private void ResetButton_MouseLeave( object sender, MouseEventArgs e ) {
            ResetButton.Source = new BitmapImage( new Uri( @"Images/Buttons/ResetAllBtnRest.png", UriKind.Relative ) );
        }

        private void ResetButton_MouseLeftButtonDown( object sender, MouseButtonEventArgs e ) {
            
        }

        private void LinkButton_MouseEnter( object sender, MouseEventArgs e ) {
            ImageBrush img = new ImageBrush();
            img.ImageSource = new BitmapImage( new Uri( @"Images/Buttons/ResetAllBtnHover.png", UriKind.Relative ) );
            LinkButton.Background = img;
        }

        private void LinkButton_MouseLeave( object sender, MouseEventArgs e ) {
            ImageBrush img = new ImageBrush();
            img.ImageSource = new BitmapImage( new Uri( @"Images/Buttons/ResetAllBtnRest.png", UriKind.Relative ) );
            LinkButton.Background = img;
        }

        private void LinkButton_MouseLeftButtonDown( object sender, MouseButtonEventArgs e ) {
            if( _talents != null ) {
                string address = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString() + "#";
                System.Windows.Browser.HtmlPage.Window.Navigate( new Uri( address + _talents.ToString(), UriKind.Absolute ), "_blank" );
            }

            else
                System.Windows.Browser.HtmlPage.Window.Alert( "You haven't chosen a talent spec" );
        }

        private void LinkButton_Click( object sender, RoutedEventArgs e ) {
            string address = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString() + "#";
            LinkButton.NavigateUri = new Uri( address + _talents.ToString(), UriKind.Absolute );
        }

        private void StartAnimations() {
            if( TreeOne.IsHidden ) {
                foreach( UIElement ue in this.LayoutRoot.Children ) {
                    // Increase the opacity of the talent trees to 1
                    if( ue is TalentTree )
                        ( ue as TalentTree ).StartAnimation();

                    // Reduce the size of the AC buttons to 0
                    if( ue is ClassButton )
                        ( ue as ClassButton ).StartAnimation();
                }

                ClassBtnAnimation.Begin();
            }
        }

        private void ChangeAddress() {
            if( _talents == null ) {
                AddressBox.Text = "www.PlaceholderForDebugging.com/talentcalc"; // Address.BaseAddress
            }
            else {
                AddressBox.Text = "www.PlaceholderForDebuggeing.com/talentcalc" + "#" + _talents.ToString(); // Address.BaseAddress
            }
        }

        private void AdvClassButton_OnClick( object sender, MouseButtonEventArgs e ) {
            ClassButton cb = sender as ClassButton;

            switch( cb.Name ) {
                case "btnGuard": case "popBtnGuard":
                    // Create GuardTalents object
                    knightPopup.IsOpen = false;
                    return;

                case "btnSent": case "popBtnSent":
                    // Create SentinelTalents object
                    knightPopup.IsOpen = false;
                    return;

                case "btnSage": case "popBtnSage":
                    // Create SageTalents object
                    consularPopup.IsOpen = false;
                    return;

                case "btnShadow": case "popBtnShadow":
                    // Create ShadowTalents object
                    consularPopup.IsOpen = false;
                    return;

                case "btnScoundrel": case "popBtnScoundrel":
                    // create ScoundrelTalents object
                    smugglerPopup.IsOpen = false;
                    return;

                case "btnSlinger": case "popBtnSlinger":
                    // create GunslingerTalents object
                    smugglerPopup.IsOpen = false;
                    return;

                case "btnVanguard": case "popBtnVanguard":
                    // create VanguardTalents object
                    trooperPopup.IsOpen = false;
                    return;

                case "btnCommand": case "popBtnCommand":
                    // create CommandoTalents object
                    trooperPopup.IsOpen = false;
                    return;

                case "btnJugger": case "popBtnJugger":
                    // create MarauderTalents object
                    warriorPopup.IsOpen = false;
                    return;

                case "btnMaraud": case "popBtnMaraud":
                    // create JuggernautTalents object
                    warriorPopup.IsOpen = false;
                    return;

                case "btnSorc": case "popBtnSorc":
                    SetClassTalents( new SorcererTalents(), true );
                    inquisPopup.IsOpen = false;
                    break;

                case "btnAss": case "popBtnAss":
                    SetClassTalents( new AssassinTalents(), false );
                    inquisPopup.IsOpen = false;
                    break;

                case "btnSnipe": case "popBtnSnipe":
                    // create SniperTalents object
                    agentPopup.IsOpen = false;
                    return;

                case "btnOper": case "popBtnOper":
                    // create OperativeTalents object
                    agentPopup.IsOpen = false;
                    return;

                case "btnMerc":
                    // create MercenaryTalents object
                    hunterPopup.IsOpen = false;
                    return;

                case "btnPower":
                    // create PowertechTalents object
                    return;
            }

            TalentsChanged( sender, EventArgs.Empty );
            StartAnimations();
            ChangeAddress();
        }

        private void ClassLabel_OnClick( object sender, MouseButtonEventArgs e ) {
            Label cl = sender as Label;

            if( _talents != null ) {
                switch( cl.Name ) {
                    case "lblKnight":
                        knightPopup.IsOpen = !knightPopup.IsOpen;
                        ClosePopUps( knightPopup );
                        break;

                    case "lblConsular":
                        consularPopup.IsOpen = !consularPopup.IsOpen;
                        ClosePopUps( consularPopup );
                        break;

                    case "lblSmuggler":
                        smugglerPopup.IsOpen = !smugglerPopup.IsOpen;
                        ClosePopUps( smugglerPopup );
                        break;

                    case "lblTrooper":
                        trooperPopup.IsOpen = !trooperPopup.IsOpen;
                        ClosePopUps( trooperPopup );
                        break;

                    case "lblWarrior":
                        warriorPopup.IsOpen = !warriorPopup.IsOpen;
                        ClosePopUps( warriorPopup );
                        break;

                    case "lblInquisitor":
                        inquisPopup.IsOpen = !inquisPopup.IsOpen;
                        ClosePopUps( inquisPopup );
                        break;

                    case "lblAgent":
                        agentPopup.IsOpen = !agentPopup.IsOpen;
                        ClosePopUps( agentPopup );
                        break;

                    case "lblHunter":
                        hunterPopup.IsOpen = !hunterPopup.IsOpen;
                        ClosePopUps( hunterPopup );
                        break;
                }
            }
        }

        private void ClosePopUps( Popup staysOpen ) {
            if( staysOpen.Name != "knightPopup" )
                knightPopup.IsOpen = false;

            if( staysOpen.Name != "consularPopup" )
                consularPopup.IsOpen = false;

            if( staysOpen.Name != "smugglerPopup" )
                smugglerPopup.IsOpen = false;

            if( staysOpen.Name != "trooperPopup" )
                trooperPopup.IsOpen = false;

            if( staysOpen.Name != "warriorPopup" )
                warriorPopup.IsOpen = false;

            if( staysOpen.Name != "inquisPopup" )
                inquisPopup.IsOpen = false;

            if( staysOpen.Name != "agentPopup" )
                agentPopup.IsOpen = false;

            if( staysOpen.Name != "hunterPopup" )
                hunterPopup.IsOpen = false;
        }
    }
}
